# configBusiness.py
from datetime import datetime
from flask import jsonify
from https.httputils import DataManager



class template:
    def __init__(self, bqb_url, bqb_writing, bqb_look, bqb_back, bqb_music, upload_address, bqb_virtually,
                 bqb_typeface, template_id):
        self.bqb_url = bqb_url
        # 文字
        self.bqb_writing = bqb_url.format(bqb_writing)
        # 表情
        self.bqb_look = bqb_url.format(bqb_look)
        # 背景
        self.bqb_back = bqb_url.format(bqb_back)
        # 音乐
        self.bqb_music = bqb_url.format(bqb_music)
        self.upload_address = upload_address
        # 头像
        self.bqb_virtually = bqb_url.format(bqb_virtually)
        # 字体
        self.bqb_typeface = bqb_url.format(bqb_typeface)
        # 模板id
        self.template_id = template_id


# 创建一个 AiMake 实例，表示配置信息
template_config = template(
    bqb_url="https://core.kaixinchuichui.cn/admin/index.php/api/bqbsource/page?name={}&page=1&page_size=10000",
    bqb_writing="bqb_writing",
    bqb_look="bqb_look",
    bqb_back="bqb_back",
    bqb_music="bqb_music",
    bqb_virtually="bqb_virtually",
    bqb_typeface="bqb_typeface",
    upload_address="https://core.kaixinchuichui.cn/admin/index.php/api/bqbsource/addvideo",
    template_id={

        51: {
            #   gif_number gif数量  ，time_len视频时长  ，x_number:X轴数量 y_number:Y轴数量 w_pro:图片W比例 h_pro:图片高比例
            1: {"gif_number": 16, "x_number": 4, "y_number": 4, "w_pro": 4, "h_pro": 4},
            2: {"gif_number": 9,  "x_number": 3, "y_number": 3, "w_pro": 2.5, "h_pro": 2.5},
            3: {"gif_number": 12, "x_number": 3, "y_number": 4, "w_pro": 3, "h_pro": 3},
            4: {"gif_number": 15, "x_number": 3, "y_number": 5},
            5: {"gif_number": 17, "x_number": 4, "y_number": 5},
            6: {"gif_number": 17, "x_number": 4, "y_number": 5},
            7: {"gif_number": 20},
            8: {"gif_number": 15, "x_number": 3, "y_number": 5},
        },
        4: {
            # 字体
            "fontSize": 80,
            # 每行字数
            "max_chars_per_line": 12,
            # 语音生成语速
            "tts_speed_ratio": 0.7,
            # 背景音乐音量
            "clip_background_music_volume": 0.4,
            # 背景语音音量
            "clip_background_text_volume": 1,
            # 语音类型
            "VoiceType": "BV405_streaming",
            # 情绪语音
            "EmotionCategory": {
                "通用": "",
                "愉悦": "pleased",
                "高兴": "happy",
                "抱歉": "sorry",
                "严肃": "serious",

            }
        },

        54: {"gif_number": 3, "time_len": 7,
             # 文案场景
             "copy_scene": "情侣",
             # 微信聊天页面名称字体
             "font_ttf_name_id": 4,
             # 对话字体
             "font_ttf_session_id": 5,
             # 气泡
             "bubble": {
                 "fontSize": 50,
             },
             # 头像属性： 大小       弧度
             "head": {"wh": 120, "radius": 15},
             # 头部gif属性
             "top_gif_max": 320,
             # 中部gif属性
             "bottom_gif_max": 400,
             # 低部gif属性
             "center_gif_max": 320
             },
        16: {
            # 情侣文案
            "copy_scene": "情侣",
            # gif数量
            "gif_number": 1,
            # 视频总时长
            "time_len": 10,
            # gif 大小
            "w_h": 500,
            # 颜色
            "color": "yellow",
            # 字体大小
            "fontSize": 50,
            "positions_top": [(400, 650)],
            "positions_left": [(150, 750), (150, 900), (150, 1050), (150, 1200)],
            "positions_right": [(600, 750), (600, 900), (600, 1050), (600, 1200)],

        },
        47: {
            "time_len": 6,
            "gif_number": 40,
            # gif 大小
            "w_h": 500,
            "fontSize": 100,
            "duration": 0.05,
            # 尾部预留时长
            "last_time": 0

        },
        48: {"gif_number": 6,
             "x_number": 2,
             "y_number": 3,
             "w_pro": 1.2,
             "h_pro": 1.2,
             "time_len": 11,
             },
        49: {"gif_number": 4,
             "x_number": 2,
             "y_number": 2,
             "time_len": 9,
             },
    }

)


def get_all_to_url(url):
    try:
        data_manager = DataManager(api_url=url)
        response_data = data_manager.get_url_data()

        if response_data.code == 0:
            list_data = response_data.data.get('list', {}).get('list', [])

            return list_data
        return jsonify({'error': 'Failed to fetch data'})
    except Exception as e:
        return jsonify({'error': str(e)})


def get_list_gif(all_to_url):
    # 使用列表推导式筛选出 'type' 等于 '动图' 的元素
    list_gif = [item for item in all_to_url if item.get('type') == '动图']

    return list_gif


def get_list_gif_2(url):
    list_data = []
    try:
        data_manager = DataManager(api_url=url)
        response_data = data_manager.get_url_data()

        # if response_data.code == 1:
        #     list_data = [item for item in response_data.data if item.get('type') == 'erchuang']
        return response_data.data
    except Exception as e:
        return []


def get_list_png_time(all_to_url):
    # current_time = datetime.now().strftime('%Y%m%d')
    # current_time = ["20240109", "20240111"]
    # list_png = [item for item in all_to_url if all(date not in item.get('src', '').replace('-', '') for date in current_time) and item.get('id') != 3]
    list_png = [item for item in all_to_url if item.get('id') != 3]
    return list_png


def get_list_music_time(all_to_url):
    # current_time = datetime.now().strftime('%Y%m%d')
    # current_time = ["20240109", "20240111"]
    # list_png = [item for item in all_to_url if all(date not in item.get('src', '').replace('-', '') for date in current_time) and item.get('id') != 3]
    list_png = [item for item in all_to_url if datetime.fromtimestamp(item.get('time')) > datetime(2024, 3, 31)]

    return list_png


def select_by_model_id(model_id, data_list):
    filtered_list = [item for item in data_list if item.get('model_id') == model_id]
    return filtered_list


def request_body_dict(text):
    request_body_dict = {
        "messages": [
            {
                "role": "user",
                "content": text
            }
        ],
        "member_id": 1,
        "pwd": "oivu2398a03s85250hs928"
    }
    return request_body_dict


if __name__ == '__main__':
    print(datetime.now().strftime('%Y%m%d'))
